package com.example.qqfake;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LoginActivity extends AppCompatActivity {

    private Client1 client1;
    //定义登录Button 编辑框
    private Button btn_login;
    private EditText et_password,et_userName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ListView test_text=(ListView)findViewById(R.id.test_text);


        // 获取SharedPreferences实例，文件名为"myPreferences"
        SharedPreferences sharedPreferences = getSharedPreferences("myPreferences", Context.MODE_PRIVATE);
        // 获取SharedPreferences.Editor对象来编辑数据
        SharedPreferences.Editor editor = sharedPreferences.edit();




        //初始化
        initView();
        //注册完之后更新

        /*点击跳转至注册页面 【还没有账号？点击注册】按钮*/
        Button btn_register1=(Button)findViewById(R.id.btn_register1);
        btn_register1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击按钮跳转到注册页面
                Intent intent = new Intent(getApplicationContext(), RegisterActivity.class);
                //注册返回代码为0时，跳转
                startActivity(intent);
            }
        });

        //登录按钮单击事件
        btn_login=(Button)findViewById(R.id.btn_login);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取输入的密码框内容
                String etpassword=et_password.getText().toString();
                String md5String = MD5.getMD5(etpassword);


                /*获取数据库里的数据*/
                //登录按钮获取要查询的账号
                String id=et_userName.getText().toString();

                //子线程开启socket
                new Thread() {
                    private BufferedReader bufferedReader = null;
                    public void run() {
                        //发送socket
                        try {
                            client1 = new Client1();
                            String content1 = null;
                            bufferedReader = new BufferedReader(new InputStreamReader(client1.socket.getInputStream()));

                            Send(id+" "+md5String);


                            while((content1 = bufferedReader .readLine()) != null) {
                                String finalContent = content1;
                                if(!isTelPhoneNumber(id)||finalContent.equals("false1")){
                                    if(!isFinishing()){
                                        runOnUiThread(new Runnable() {//此run方法将由主线程运行
                                            @Override
                                            public void run() {
                                                Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
                                            }
                                        });
                                    }break;
                                }else{
                                    if(!isFinishing()){
                                        runOnUiThread(new Runnable() {//此run方法将由主线程运行

                                            @Override
                                            public void run() {
                                                // 保存数据，键为"exampleKey"，值为"exampleValue"
                                                editor.putString("User_id", id);
                                                // 提交修改
                                                editor.apply();


                                                Toast.makeText(LoginActivity.this,"登录成功！",Toast.LENGTH_SHORT).show();
                                                //跳转到MainActivity
                                                Intent intent=new Intent(LoginActivity.this,MainActivity.class);

                                                startActivity(intent);
                                                //关闭登录页面
                                                LoginActivity.this.finish();

                                            }
                                        });
                                    }

                                }
                            }

                        } catch (IOException e) {
                            e.printStackTrace();
                        }finally{
                            try {
                                if(bufferedReader!=null){
                                    bufferedReader.close();
                                }
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            }
                        }
                    }
                }.start();

            }
        });
    }
    /*正则化验证账号*/
    public static boolean isTelPhoneNumber(String mobile) {
        if (mobile != null ) {
            Pattern pattern = Pattern.compile("^\\d+$");
            Matcher matcher = pattern.matcher(mobile);
            return matcher.matches();
        }else{
            return false;
        }
    }
    //定义初始化
    private void initView(){
        btn_login=findViewById(R.id.btn_login);
        et_userName=findViewById(R.id.et_username);
        et_password=findViewById(R.id.et_password);
    }


    //发送登录验证信息
    public void Send(String content){
        try {
            //发送消息
            client1.send(String.valueOf(0),String.valueOf(0),content);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    //    //重写onDestroy()方法
    @Override
    protected void onDestroy() {
        super.onDestroy();
        try{
            //关闭输入流，socket连接
            if(client1.socket!=null){
                client1.socket.close();
            }

        }catch(IOException e){
            e.printStackTrace();

        }

    }
}
